package com.zbkj.crmeb.merchant.admin.controller;

import com.common.CommonPage;
import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.crmeb.core.exception.CrmebException;
import com.zbkj.crmeb.front.request.ProductRequest;
import com.zbkj.crmeb.store.model.StoreBanner;
import com.zbkj.crmeb.store.model.StoreZone;
import com.zbkj.crmeb.store.model.StoreZoneProduct;
import com.zbkj.crmeb.store.request.StoreZoneProductRequest;
import com.zbkj.crmeb.store.response.StoreProductResponse;
import com.zbkj.crmeb.store.service.StoreBannerService;
import com.zbkj.crmeb.store.service.StoreProductService;
import com.zbkj.crmeb.store.service.StoreZoneProductService;
import com.zbkj.crmeb.store.service.StoreZoneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

/**
 * 专区-banner
 * @author muyu.myw
 * @since 2020-09-17
 */
@Slf4j
@RestController
@RequestMapping("api/admin/mer/store/zoneAndBanner")
@Api(tags = "专区-banner管理")
public class MerStoreZoneBannerController extends AdminBaseController {

    @Autowired
    private StoreZoneProductService storeZoneProductService;

    @Autowired
    private StoreZoneService storeZoneService;

    @Autowired
    private StoreBannerService storeBannerService;

    @Autowired
    private StoreProductService storeProductService;


    /**
     * 分页显示专区
     * @param storeZone 搜索条件
     * @param pageParamRequest 分页参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "分页列表") //配合swagger使用
    @RequestMapping(value = "/zone/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreZone>> getStoreZoneList(
            @Validated StoreZone storeZone, @Validated PageParamRequest pageParamRequest, HttpServletRequest request){
        storeZone.setMerId(getMerId(request));
        return CommonResult.success(CommonPage.restPage(storeZoneService.getList(storeZone,pageParamRequest)));
    }

    /**
     * 分页显示专区关联的商品列表
     * @param request 搜索条件
     * @param pageParamRequest 分页参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "分页列表") //配合swagger使用
    @RequestMapping(value = "/zone/goodsList", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreProductResponse>> getStoreZoneGoodsList(
            @Validated ProductRequest request, @Validated PageParamRequest pageParamRequest,HttpServletRequest servletRequest){
        request.setMerId(getMerId(servletRequest));
        return CommonResult.success(CommonPage.restPage(storeProductService.selectZoneProductList(request,pageParamRequest)));
    }

    /**
     * 新增专区
     * @param storeZone
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "新增专区") //配合swagger使用
    @RequestMapping(value = "/zone/add", method = RequestMethod.POST)
    public CommonResult<Integer> addStoreZone(@Validated StoreZone storeZone, HttpServletRequest request){
        storeZone.setIsDel(0);
        storeZone.setCreateTime(new Date());
        storeZone.setMerId(getMerId(request));
        return CommonResult.success(storeZoneService.saveStoreZone(storeZone));
    }

    /**
     * 编辑专区
     * @param storeZone
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "编辑专区") //配合swagger使用
    @RequestMapping(value = "/zone/update", method = RequestMethod.POST)
    public CommonResult<Integer> udpateStoreZone(HttpServletRequest request,@Validated StoreZone storeZone){
        if(Objects.isNull(storeZone.getId())){
            return CommonResult.failed("编辑失败!");
        }
        storeZone.setMerId(getMerId(request));
        return CommonResult.success(storeZoneService.update(storeZone));
    }

    /**
     * 加入专区
     * @param request
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "加入专区") //配合swagger使用
    @RequestMapping(value = "/zone/product/add", method = RequestMethod.POST)
    public CommonResult<Boolean> addZoneProduct(@Validated StoreZoneProductRequest request,HttpServletRequest servletRequest){
        if(StringUtils.isBlank(request.getProductIds())){
            return CommonResult.failed("商品id必传!");
        }
        request.setMerId(getMerId(servletRequest));
        return CommonResult.success(storeZoneProductService.saveBatch(request));
    }

    /**
     * 移除专区
     * @param request
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "移除专区") //配合swagger使用
    @RequestMapping(value = "/zone/product/del", method = RequestMethod.POST)
    public CommonResult<Integer> delZoneProduct(@Validated StoreZoneProductRequest request,HttpServletRequest servletRequest){
        request.setMerId(getMerId(servletRequest));
        return CommonResult.success(storeZoneProductService.delete(request));
    }

    /**
     * 分页显示专区
     * @param storeBanner 搜索条件
     * @param pageParamRequest 分页参数
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "分页列表") //配合swagger使用
    @RequestMapping(value = "/banner/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreBanner>> getStoreBannerList(
            @Validated StoreBanner storeBanner, @Validated PageParamRequest pageParamRequest, HttpServletRequest request){
        storeBanner.setMerId(getMerId(request));
        return CommonResult.success(CommonPage.restPage(storeBannerService.getList(storeBanner,pageParamRequest)));
    }

    /**
     * 新增专区
     * @param storeBanner
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "新增专区") //配合swagger使用
    @RequestMapping(value = "/banner/add", method = RequestMethod.POST)
    public CommonResult<Integer> addStoreZone(@Validated StoreBanner storeBanner, HttpServletRequest request){
        if(null != storeBanner && null == storeBanner.getType() ){
            throw new CrmebException("跳转类型不能为空!");
        }
        //storeBanner.setStatus(0);
        storeBanner.setIsDel(0);
        storeBanner.setCreateTime(new Date());
        storeBanner.setMerId(getMerId(request));
        return CommonResult.success(storeBannerService.saveStoreBanner(storeBanner));
    }

    /**
     * 编辑专区
     * @param storeBanner
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "编辑专区") //配合swagger使用
    @RequestMapping(value = "/banner/update", method = RequestMethod.POST)
    public CommonResult<Integer> udpateStoreZone(@Validated StoreBanner storeBanner){
        if(Objects.isNull(storeBanner.getId())){
            return CommonResult.failed("编辑失败!");
        }
        return CommonResult.success(storeBannerService.update(storeBanner));
    }

    /**
     * 置顶
     * @param request
     * @author cyz
     * @since 2021-04-22
     */
    @ApiOperation(value = "置顶") //配合swagger使用
    @RequestMapping(value = "/zoneProduct/update", method = RequestMethod.POST)
    public CommonResult<Boolean> updateZoneProduct(@Validated StoreZoneProductRequest request){
        return CommonResult.success(storeZoneProductService.update(request));
    }

    /**
     * 获取导航栏专区是否显示
     * @param
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "获取导航栏专区是否开启") //配合swagger使用
    @RequestMapping(value = "/zone/getAppBarInfo", method = RequestMethod.GET)
    public CommonResult<Integer> getAppBarInfo(HttpServletRequest request){
        Integer merId = getMerId(request);
        return CommonResult.success(storeZoneService.getAppBarInfo(merId));
    }

    /**
     * 获取导航栏专区是否显示
     * @param
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "修改导航栏专区是否开启") //配合swagger使用
    @RequestMapping(value = "/zone/updateAppBarStatus/{status}", method = RequestMethod.GET)
    public CommonResult<Boolean> updateAppBarStatus(HttpServletRequest request,@PathVariable("status") String status){
        Integer merId = getMerId(request);
        return CommonResult.success(storeZoneService.updateAppBarStatus(merId,status));
    }

    @ApiOperation(value = "批量移除专区商品")
    @RequestMapping(value = "/zone/product/delBatchZoneProduct", method = RequestMethod.POST)
    public CommonResult<Integer> delBatchZoneProduct(@RequestBody StoreZoneProductRequest request, HttpServletRequest servletRequest){
        request.setMerId(getMerId(servletRequest));
        return CommonResult.success(storeZoneProductService.delBatchZoneProduct(request));
    }
}

